#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define QNO 12

#define CNO 4

#define swap(type, a, b) do{ type x=a; a=b; b=x; }while(0)

char *cptr[] = {
    "动物",     "汽车",     "花",   "家",   "桌子",     "书",
    "椅子",     "爸爸",     "妈妈", "爱",   "和平",     "杂志",
};

char *eptr[] = {
    "animal",   "car",      "flower","house","desk",    "book",
    "chair",    "father",   "mother","love", "peace",   "magazine",
};

void print_cand(const int *c, int sw)
{
    for (int i=0; i<CNO; ++i) {
        printf("(%d)%-12s", i+1, sw ? cptr[c[i]] : eptr[c[i]]);
    }
}

void make_cand(int *c, int n)
{

    int book[QNO] = {0};    //用来标识哪些单词已经使用了
    book[n] = 1;

    c[0] = n;

    for (int i=1; i<CNO; ++i) {
        int val;
        do {
            val = rand() % QNO;
        } while (book[val]);

        book[val] = 1;
        c[i] = val;
    }
}

int main(void)
{
    int i;

    int nq, pq;

    int na;     //正确答案的编号

    int sw;     //0中文 1英文

    int retry;
    int cand[CNO];  //选项索引值

    srand(time(NULL));

    pq = QNO;

    do {

        do {
            nq = rand() % QNO;
        } while (nq == pq);

        sw = rand() % 2;

        make_cand(cand, nq);

        for (i=CNO-1; i>0; --i) {
            int s = rand() % (i+1);
            swap(int, cand[s], cand[i]);
        }

        printf("哪一个是%s?\n", sw? eptr[nq]: cptr[nq]);

        print_cand(cand, sw);

        putchar('\n');

        for (int i=0; i<CNO; ++i) {
            if (cand[i] == nq) {
                na = i+1;
                break;
            }
        }

        int no;
        do {
            printf("请回答: ");

            scanf("%d", &no);
            if (no != na) {
                printf("回答错误.\n");
            }
        } while (no != na);

        pq = nq;

        puts("回答正确.");

        printf("再来一次?..0-否/1-是: ");

        scanf("%d", &retry);
    } while (retry == 1);

    return 0;
}
